﻿using huqiang;
using huqiang.Core.HGUI;
using huqiang.Core.UIData;
using huqiang.Data;
using huqiang.Data.Extension;
using huqiang.Helper.HGUI;
using huqiang.UIComposite;
using System;
using System.Collections.Generic;
using UnityEngine;

public class DockPanelHelper : UIHelper
{
    public bool Preview;
    public SplitView dockPanel;
    [TextArea(20,40)]
    public string Layout;
    public override void Refresh()
    {
        if(Preview)
        {
            ParseLayout();
        }
    }
    List<CreateCmd> ParseLayout()
    {
        string t = Layout.Replace(" ","");
        string[] ss= t.Split('\r','\n');
        List<string> tmp = new List<string>();
        List<CreateCmd> cmds = new List<CreateCmd>();
        CreateCmd cc = new CreateCmd();
        for (int i = 0; i < ss.Length; i++)
        {
            string str = ss[i];
            if(str!="")
            {
                string[] cs= str.Split('=');
                if(cs.Length>1)
                {
                    string cmd = cs[0];
                    if(tmp.Contains(cmd))
                    {
                        GetCmd(cs,ref cc);
                    }
                    else
                    {
                        cmds.Add(cc);
                        cc = new CreateCmd();
                        tmp.Clear();
                    }
                    tmp.Add(cmd);
                }
            }
        }
        if (tmp.Count > 0)
            cmds.Add(cc);
        return cmds;
    }
    void ExecuteCmd(List<CreateCmd> cmds)
    {
        dockPanel = new SplitView();
        DataBuffer db = new DataBuffer();
        FakeStruct fs = new FakeStruct(db,2);
        dockPanel.Initial(fs,GetComponent<UIContext>().GetUIData(),null);
    }

    void GetCmd(string[] cs,ref  CreateCmd cc)
    {
        switch(cs[0])
        {
            case "r":
                float.TryParse(cs[1],out cc.r);
                break;
            case "w":
                float.TryParse(cs[1],out cc.w);
                break;
            case "n":
                cc.n = cs[1];
                break;
            case "pn":
                cc.pn = cs[1];
                break;
            case "c":
                cc.color = GetColor(cs[1]);
                break;
        }
    }
    Color32 GetColor(string str)
    {
        switch (str)
        {
            case "white": return Color.white;
            case "green": return Color.green;
            case "cyan": return Color.cyan;
            case "clear": return Color.clear;
            case "grey": return Color.grey;
            case "gray": return Color.gray;
            case "magenta": return Color.magenta;
            case "red": return Color.red;
            case "yellow": return Color.yellow;
            case "black": return Color.black;
            case "blue": return Color.blue;
            default:
                {
                    string[] ss = str.Split(',');
                    if(ss.Length>1)
                    {
                        Color32 col = new Color32(0, 0, 0, 255);
                        byte.TryParse(ss[0],out col.r); 
                        byte.TryParse(ss[1],out col.g);
                        if(ss.Length>2)
                        {
                            byte.TryParse(ss[2], out col.b);
                            if (ss.Length>3)
                            {
                                byte.TryParse(ss[3], out col.a);
                            }
                        }
                        return col;
                    }
                    else
                    {
                        int a = Convert.ToInt32(str, 16);
                        return a.ToColor();
                    }
                }
        }
    }
    struct CreateCmd
    {
        public string pn;
        public string n;
        public float r;
        public float w;
        public Color32 color;
        public DockpanelArea.Dock dock;
        public CreateCmd[] child;
    }
    //{{r:0.5,dock:left,child:{}}{}}
    public override unsafe void ToBufferData(DataBuffer db, UIElementData* data)
    {
        if(dockPanel!=null)
        {
            var fs = dockPanel.SaveToDataBuffer(db);
            data->ex = db.AddData(fs, DataType.FakeStruct);
        }
    }
}